package com.toKnow.controller;

import com.toKnow.model.entity.Column;
import com.toKnow.model.vo.BasicVo;
import com.toKnow.model.vo.ColumnVo;
import com.toKnow.service.ColumnService;
import com.toKnow.service.ServiceException;
import com.toKnow.utils.JwtUserSessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/api/column")
public class ColumnController {
    @Autowired
    ColumnService columnService;
    public Integer getUid(HttpSession session){
        try{
            String token = (String) session.getAttribute("userToken");
            return Integer.parseInt(JwtUserSessionUtil.getInfo(token,"id"));
        } catch (Exception e){
            return null;
        }
    }
    @ResponseBody
    @RequestMapping(value = "/content")//,method = RequestMethod.POST)
    public BasicVo getSource(HttpSession session,String id){
        if (id==null) return new BasicVo(false,"没有携带专栏编号",null);
        //estHelpUtil.fakeLogin(session,1);
        try{
            Integer cid=Integer.parseInt(id);
            Integer uid=getUid(session);
            Column column = columnService.getColumn(cid);
            if(column==null) return new BasicVo(false,"没有找到资源",null);
            ColumnVo columnVo = new ColumnVo(column);
            if (uid!=null){
                columnVo.setIs_collect(columnService.isCollectColumn(uid,cid));
            }
            return new BasicVo(true,null,columnVo);
        }catch (NumberFormatException e){
            return new BasicVo(false,"资源编号不为数字",null);
        }
        catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
//    @ResponseBody
//    @RequestMapping(value = "/content/browser",method = RequestMethod.POST)
//    public BrowseVo browseColumn(String userid, String columnid){
//        BrowseVo browseVO = new BrowseVo();
//        if (userid==null||columnid==null) return null;
//        Integer uid=Integer.parseInt(userid),aid=Integer.parseInt(columnid);
//        try{
//            browseVO.setIscollect(columnService.isCollectColumn(uid,aid));
//            return browseVO;
//        }catch (Exception e){
//            return null;
//        }
//    }
    @ResponseBody
    @RequestMapping(value = "/content/collect",method = RequestMethod.POST)
    public BasicVo collectArticle(HttpSession session, String column_id){
        if (column_id==null) return new BasicVo(false,"没有携带文章编号",null);
        // TestHelpUtil.fakeLogin(session,1);
        try{
            Integer aid=Integer.parseInt(column_id);
            Integer uid=getUid(session);
            if (uid!=null){
                if (columnService.changeCollectColumn(uid,aid)) return new BasicVo(true,null,null);
                else throw new RuntimeException("未知错误");
            }else {
                return new BasicVo(false,"没有找到用户ID，可能是没有登录",null);
            }
        }catch (NumberFormatException e){
            return new BasicVo(false,"专栏编号不为数字",null);
        }catch (ServiceException e){
            return new BasicVo(false,e.getMessage(),null);
        } catch (Exception e){
            return new BasicVo(false,"未知错误",null);
        }
    }
}
